package com.mengfou;

import com.android.build.gradle.AppExtension;
import com.android.build.gradle.api.ApplicationVariant;
import com.mengfou.tasks.AnnotationTask;

import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskOutputFilePropertyBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Objects;

/**
 * @author 梦否 on 2023/1/14
 * @blog https://mengfou.blog.csdn.net/
 */
public class AnnotationPlugin extends BasePlugin {

    @Override
    public void apply(Project project) {
        AppExtension appExtension = project.getExtensions().getByType(AppExtension.class);
        project.afterEvaluate(new Action<Project>() {
            @Override
            public void execute(Project project) {
                for (ApplicationVariant applicationVariant : appExtension.getApplicationVariants()) {
                    Collection<File> javaDirectories = applicationVariant.getSourceSets().get(0).getJavaDirectories();
                    for (File javaDirectory : javaDirectories) {
                        doProcess(javaDirectory, project, applicationVariant);
                    }
                }
            }
        });
    }

    private void doProcess(File javaDirectory, Project project, ApplicationVariant applicationVariant) {
        if(javaDirectory.isDirectory()) {
            for (File file : Objects.requireNonNull(javaDirectory.listFiles())) {
                doProcess(file, project, applicationVariant);
            }
            return;
        }
        doProcessJavaFile(javaDirectory, project, applicationVariant);
    }

    private void doProcessJavaFile(File file, Project project, ApplicationVariant applicationVariant) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            int len = -1;
            byte[] buffer = new byte[1024];
            StringBuilder stringBuilder = new StringBuilder();
            while((len = fileInputStream.read(buffer)) != -1) {
                stringBuilder.append(new String(buffer, 0, len));
            }
            boolean flag = stringBuilder.toString().contains("@MFLog");
            if(flag) {
                createProcessTask(file, project, applicationVariant);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void createProcessTask(File file, Project project, ApplicationVariant applicationVariant) {
        AnnotationTask annotationTask = project.getTasks().create(getTaskName("annotation", "Process", applicationVariant.getName()), AnnotationTask.class);
        String a = project.getBuildDir().getAbsolutePath() +
                File.separator +
                "generated" +
                File.separator +
                "source" +
                File.separator +
                "buildConfig" +
                File.separator +
                applicationVariant.getBuildType().getName() +
                File.separator +
                "com/weizu/hello";
        annotationTask.getInputs().file(file);
        annotationTask.getOutputs().file(a);
        applicationVariant.registerJavaGeneratingTask(annotationTask, annotationTask.getOutputs().getFiles().getFiles());
    }
}
